<template>
	<view>
		<view class="box3" style="margin-top:60rpx;">
				<view class="items">
					<text>账号：</text>
				  <input class="uni-input" disabled="disabled"  focus placeholder="请输入手机号码"   v-model="submitParams.username"/>
				</view>
				<view class="items" style="margin-top: 30rpx;justify-content: space-between;">
				  <view class="" style="display: flex;justify-content: center;">
				   	<text>旧密码：</text>
				    <input class="uni-input input2"  @input="handleInput"  maxlength="12" placeholder="请输入旧密码" v-model="submitParams.usedpassword">
				  </view>
	
				</view>
				
				<view class="items" style="margin-top: 30rpx;justify-content: space-between;">
				  <view class="" style="display: flex;justify-content: center;">
				   	<text>新密码：</text>
				    <input class="uni-input input2" @input="handleInput2" maxlength="12" placeholder="请输入新密码"  v-model="submitParams.password">
				  </view>
				    <text style="color: red; font-size: 25rpx;">6-12位数字字母</text>
				</view>
				<view class="items" style="margin-top: 30rpx;justify-content: space-between;">
				  <view class="" style="display: flex;justify-content: center;">
				   	<text>确认密码：</text>
				    <input class="uni-input input2" @input="handleInput3" maxlength="12" placeholder="请确认密码"  v-model="submitParams.password2">
				  </view>
				    <text style="color: red; font-size: 25rpx;">6-12位数字字母</text>
				</view>
				
				<view class="submt" style="position: relative;left: 0;top: 0;" @click="submt()">
				  <text style="font-size: 40rpx;" class="submitValue">提交</text>
				</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	var urlPrefix = app.globalData.urlPrefix;
	export default {
		data() {
			return {
				 inputValue:"",
				  submitParams: {
				   username: "",
				   usedpassword:"",
				   password:"",
				   password2:"",
				   
				   token:null
				 },
			}
		},
		
		 onLoad: function (options) {
			 var token = uni.getStorageSync('token');
			 this.token = token
			 this.submitParams.username = options.username
			 console.log("xx",options.username)
		 },
		
		methods: { 
			handleInput(event) {
			  
			   const o = event.detail.value
				const inputRule = /[^\w_]/g
				if(inputRule.test(o)){
						uni.showToast({
							title: '只能输入数字、字母或下划线',
							icon: 'none',
							duration: 2000
						})
				}
				this.$nextTick(() => {
					this.submitParams.usedpassword = o.replace(inputRule, '');
					if (this.submitParams.usedpassword == '') {
						return
					} else {
						if(inputRule.test(o)){
								uni.showToast({
									title: '只能输入数字、字母或下划线',
									icon: 'none',
									duration: 2000
								})
						}
					}
				}) 
			},
			handleInput2(event) {
			  
			   const o = event.detail.value
				const inputRule = /[^\w_]/g
				if(inputRule.test(o)){
						uni.showToast({
							title: '只能输入数字、字母或下划线',
							icon: 'none',
							duration: 2000
						})
				}
				this.$nextTick(() => {
					this.submitParams.password = o.replace(inputRule, '');
					if (this.submitParams.password == '') {
						return
					} else {
						if(inputRule.test(o)){
								uni.showToast({
									title: '只能输入数字、字母或下划线',
									icon: 'none',
									duration: 2000
								})
						}
					}
				}) 
			},
			handleInput3(event) {
			  
			   const o = event.detail.value
				const inputRule = /[^\w_]/g
				if(inputRule.test(o)){
						uni.showToast({
							title: '只能输入数字、字母或下划线',
							icon: 'none',
							duration: 2000
						})
				}
				this.$nextTick(() => {
					this.submitParams.password2 = o.replace(inputRule, '');
					if (this.submitParams.password2 == '') {
						return
					} else {
						if(inputRule.test(o)){
								uni.showToast({
									title: '只能输入数字、字母或下划线',
									icon: 'none',
									duration: 2000
								})
						}
					}
				}) 
			},
			submt(){
				const params = this.submitParams
				
				if(params.password=='' || params.password== null){
					uni.showToast({
						title: '请输入新密码',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if(params.password2=='' || params.password2== null){
					uni.showToast({
						title: '请确认密码',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if(params.usedpassword=='' || params.usedpassword== null){
					uni.showToast({
						title: '请输入旧密码',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				
				if(params.password2 != params.password){
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if(params.password.length<6){
					uni.showToast({
						title: '密码不得低于6位',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				
				
				uni.request({
					url: urlPrefix + '/api/applet/myInfo/updatePassword',
					method: 'POST',
					data: {
						username: params.username,
						usedpassword: params.usedpassword,
						password: params.password
					},
					header: {
						'content-type': 'application/json',
						'token': this.token
					},
					success: function (res) {
						if(res.data.resp_code == 1){
							uni.showToast({
								title: res.data.resp_msg,
								icon: 'none'
							});
						}
						if(res.data.resp_code == 0){
							uni.setStorageSync('username', params.username);
							// 跳转路由
							uni.showToast({
							    title: '提交成功',
							    icon: 'success',
							    duration: 2000
							});
							uni.showToast({
							              title: '重置成功',
							              	//将值设置为 success 或者直接不用写icon这个参数
							              	icon: 'success',
							              	//显示持续时间为 2秒
							              	duration: 2000
							            })
							            var timer = setInterval(()=> {
							              uni.navigateBack()
							              clearInterval(timer)
							            },2000)
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
@import './resetpassword.scss';
</style>
